package pl.edu.agh.student.bootstrap;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.transaction.Status;
import org.apache.commons.io.IOUtils;
import org.jboss.seam.annotations.In;

import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Observer;
import org.jboss.seam.log.Log;
import org.jboss.seam.transaction.UserTransaction;
import org.jbpm.api.IdentityService;

import pl.edu.agh.student.bootstrap.testdata.PersonTestdata;
import pl.edu.agh.student.logic.dao.UserDao;
import pl.edu.agh.student.logic.util.ServiceProvider;
import pl.edu.agh.student.model.Customer;
import pl.edu.agh.student.model.Gender;
import pl.edu.agh.student.model.Role;
import pl.edu.agh.student.model.User;

@Name(ApplicationBootstrap.NAME)
@Stateless
public class ApplicationBootstrapBean implements ApplicationBootstrap {

    @Logger
    private Log log;
    @EJB
    private PersonTestdata personTestdata;
    @In("entityManager")
    protected EntityManager entityManager;
    @In("serviceProvider")
    protected ServiceProvider serviceProvider;

    @Observer("org.jboss.seam.postInitialization")
    public final void init() {
        log.debug("received org.jboss.seam.postInitialization event");

        //grupy dla jbpm
        IdentityService identityService = serviceProvider.getIdentityService();
        identityService.createGroup("boss-group");
        identityService.createGroup("customer-group");
        identityService.createGroup("analyst-group");
        
        //Roles
        Role role_0 = new Role();
        role_0.setName("Admin");
        entityManager.persist(role_0);

        Role role_1 = new Role();
        role_1.setName("Szef");
        entityManager.persist(role_1);

        Role role_2 = new Role();
        role_2.setName("Analityk");
        entityManager.persist(role_2);

        Role role_3 = new Role();
        role_3.setName("Klient");
        entityManager.persist(role_3);

        //uzytkownik admin
        User user = new User();
        user.setUsername("admin");
        user.setPassword("admin");
        user.setEmail("admin@my.bank.pl");
        user.setEnabled(true);
        user.getRoles().add(role_0);
        role_0.getUsers().add(user);
        entityManager.persist(user);
        
        User user1 = new User();
        user1.setUsername("szeff");
        user1.setPassword("tajne");
        user1.setEmail("szef@my.bank.pl");
        user1.setEnabled(true);
        role_1.getUsers().add(user1);
        identityService.createUser(user1.getUsername(), user1.getUsername(), user1.getEmail());
        identityService.createMembership(user1.getUsername(), "boss-group");
        entityManager.persist(user1);
        
        User user2 = new User();
        user2.setUsername("analityk");
        user2.setPassword("tajne");
        user2.setEmail("analityk@my.bank.pl");
        user2.setEnabled(true);
        role_2.getUsers().add(user2);
        identityService.createUser(user2.getUsername(), user2.getUsername(), user2.getEmail());
        identityService.createMembership(user2.getUsername(), "analyst-group");
        entityManager.persist(user2);

        entityManager.persist(role_0);
        entityManager.persist(role_1);
        entityManager.persist(role_2);
        entityManager.flush();

        Calendar cal = new GregorianCalendar(1980, 4, 3);
        
        Customer c1 = new Customer();
        c1.setFirstname("Tomasz");
        c1.setLastname("Krzeminski");
        c1.setBirthday(cal.getTime());
        c1.setGender(Gender.MALE);
        c1.setPesel("80040312453");
        c1.getAddress().setCity("Kraków");
        c1.getAddress().setStreet("Stradomska 5a");
        c1.getAddress().setPostcode("31-068");
        
        User u1= new User();
        u1.setUsername("tomasz");
        u1.setPassword("tajne");
        u1.setEmail("tomasz@krzemiski.pl");
        u1.setEnabled(true);
        role_3.getUsers().add(u1);
        identityService.createUser(u1.getUsername(), u1.getUsername(), u1.getEmail());
        identityService.createMembership(u1.getUsername(), "customer-group");
        c1.setUser(u1);
        entityManager.persist(u1);
        entityManager.persist(role_3);
        entityManager.persist(c1);
        
        Calendar cal2 = new GregorianCalendar(1978, 5, 13);
        
        Customer c2 = new Customer();
        c2.setFirstname("Marcin");
        c2.setLastname("Sikora");
        c2.setBirthday(cal2.getTime());
        c2.setGender(Gender.MALE);
        c2.setPesel("78051323853");
        c2.getAddress().setCity("Kraków");
        c2.getAddress().setStreet("Skłodowskiej-Curie 9 lok. 5");
        c2.getAddress().setPostcode("31-025");
        
        User u2= new User();
        u2.setUsername("marcin");
        u2.setPassword("tajne");
        u2.setEmail("marcin@sikora.pl");
        u2.setEnabled(true);
        role_3.getUsers().add(u2);
        identityService.createUser(u2.getUsername(), u2.getUsername(), u2.getEmail());
        identityService.createMembership(u2.getUsername(), "customer-group");
        c2.setUser(u2);
        entityManager.persist(u2);
        entityManager.persist(role_3);
        entityManager.persist(c2);
        
        Calendar cal3 = new GregorianCalendar(1986, 7, 20);
        
        Customer c3 = new Customer();
        c3.setFirstname("Mariola");
        c3.setLastname("Kawa");
        c3.setBirthday(cal3.getTime());
        c3.setGender(Gender.FEMALE);
        c3.setPesel("86072913789");
        c3.getAddress().setCity("Kraków");
        c3.getAddress().setStreet("Karmelicka 64");
        c3.getAddress().setPostcode("31-128");
        
        User u3= new User();
        u3.setUsername("mariola");
        u3.setPassword("tajne");
        u3.setEmail("mariola@kawa.pl");
        u3.setEnabled(true);
        role_3.getUsers().add(u3);
        identityService.createUser(u3.getUsername(), u3.getUsername(), u3.getEmail());
        identityService.createMembership(u3.getUsername(), "customer-group");
        c3.setUser(u3);
        entityManager.persist(u3);
        entityManager.persist(role_3);
        entityManager.persist(c3);
        
        Calendar cal4 = new GregorianCalendar(1982, 12, 30);
        
        Customer c4 = new Customer();
        c4.setFirstname("Malgorzata");
        c4.setLastname("Chrzanoswka");
        c4.setBirthday(cal4.getTime());
        c4.setGender(Gender.FEMALE);
        c4.setPesel("82123098674");
        c4.getAddress().setCity("Kraków");
        c4.getAddress().setStreet("Dobrego Pasterza 121");
        c4.getAddress().setPostcode("31-416");
        
        User u4= new User();
        u4.setUsername("goska");
        u4.setPassword("tajne");
        u4.setEmail("goska@chrzanowska.pl");
        u4.setEnabled(true);
        role_3.getUsers().add(u4);
        identityService.createUser(u4.getUsername(), u4.getUsername(), u4.getEmail());
        identityService.createMembership(u4.getUsername(), "customer-group");
        c4.setUser(u4);
        entityManager.persist(u4);
        entityManager.persist(role_3);
        entityManager.persist(c4);
        
        entityManager.flush();
        
        log.info("insert testdata complete");

//        try
//		{
//			String sql = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("sample.sql"));
//			
//            
//			entityManager.createNativeQuery(sql).executeUpdate();
//			log.info("Seed data successfully imported");
//		}
//		catch (Exception e)
//		{
//            log.error(e);
//			log.error("Import failed. Seed data will not be available.");
//
//		}
    }
}
